package cn.tx.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.tx.business.OrderProduct;
import cn.tx.model.Supplier;
import cn.tx.query.QueryOrderProduct;
import cn.tx.service.OrderModelService;
import cn.tx.service.SupplierService;

import com.opensymphony.xwork2.ActionContext;

public class BillAction extends BaseAction {
	
	private SupplierService supplierService;
	
	private OrderModelService orderModelService;
	
	private QueryOrderProduct erpQuery = new QueryOrderProduct();
	
	private String xmlStr;
	
	
	
	
	public String getXmlStr() {
		return xmlStr;
	}

	public void setXmlStr(String xmlStr) {
		this.xmlStr = xmlStr;
	}

	public QueryOrderProduct getErpQuery() {
		return erpQuery;
	}

	public void setErpQuery(QueryOrderProduct erpQuery) {
		this.erpQuery = erpQuery;
	}

	public OrderModelService getOrderModelService() {
		return orderModelService;
	}

	public void setOrderModelService(OrderModelService orderModelService) {
		this.orderModelService = orderModelService;
	}

	public SupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}
	
	public String bill_buyBill(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		List<Supplier> slist = supplierService.listAll();
		context.put("suppliers", slist);
		List<OrderProduct> list = orderModelService.staOrderProduct(erpQuery);
		context.put("opList", list);
		//创建文档对象
		Document doc = DocumentHelper.createDocument();
		
		List<String> colorSet = new ArrayList<String>();
		for(int i = 0 ; i < 10; i++){
			colorSet.add(i+"");
		}
		colorSet.add("A");
		colorSet.add("B");
		colorSet.add("C");
		colorSet.add("D");
		colorSet.add("E");
		colorSet.add("F");
		
		//创建根节点
		Element root = doc.addElement("graph");
		root.addAttribute("caption", "订单的商品统计")
		.addAttribute("xAxisName", "商品名称")
		.addAttribute("yAxisName", "数量")
		.addAttribute("decimalPrecision", "0")
		.addAttribute("baseFontSize", "10")
		.addAttribute("formatNumberScale", "0");
		for(OrderProduct op : list){
			Collections.shuffle(colorSet);
			String color = colorSet.get(0)+colorSet.get(1)+colorSet.get(2)+colorSet.get(3)+colorSet.get(4)+colorSet.get(5);
			root.addElement("set").addAttribute("name", op.getProductName())
			.addAttribute("value", op.getProductNum()+"").addAttribute("color", color);
		}
		xmlStr = doc.asXML();
		xmlStr = xmlStr.replace("\"", "'");
		
		return SUCCESS;
	}

}
